//
//  NSMAssetController.m
//  NSMKit
//
//  Created by nsm on 2019/3/4.
//  Copyright © 2019 nani. All rights reserved.
//

#import "NSMAssetController.h"
#import "NSMAssetPickerController.h"

@interface NSMAssetCell : NSMTableViewCell
@property (nonatomic, strong) NSMImageView *coverImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@end

@implementation NSMAssetCell
- (void)setObject:(NSMAssetsGroup *)assetsGroup {
    PHAsset *asset = assetsGroup.fetchResult.lastObject;
    @nsm_weak(self)
    [_assetsManager fecthThumbnailImageForAsset:asset size:CGSizeMake(100, 100) completion:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
        @nsm_strong(self)
        self.coverImageView.image = result;
    }];
    
    self.titleLabel.text = FormatString(@"%@-%zd",
                                        assetsGroup.assetCollection.localizedTitle,
                                        assetsGroup.numberOfAssets);
}

- (void)setupSubviews {
    MyLinearLayout *layout = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
    layout.myHorzMargin = 0;
    layout.wrapContentHeight = YES;
    layout.gravity = MyGravity_Vert_Center;
    layout.cacheEstimatedRect = YES;
    layout.padding = UIEdgeInsetsMake(8, 15, 8, 15);
    layout.subviewHSpace = 12;
    [self.contentView addSubview:layout];
    self.rootLayout = layout;
    
    NSMImageView *cover = NSMImageView.new;
    cover.size = CGSizeMake(72, 72);
    cover.backgroundColor = GROUP_TABLECOLOR;
    [layout addSubview:cover];
    _coverImageView = cover;
    
    UILabel *label = UILabel.new;
    label.weight = 1;
    label.wrapContentHeight = YES;
    label.font = CustomFont(14);
    label.textColor = COLOR_0;
    [layout addSubview:label];
    _titleLabel = label;
}
@end

@interface NSMAssetController ()
@property (nonatomic, strong) NSMutableArray *albums;
@end

@implementation NSMAssetController

#pragma mark - lifeCycle

#pragma mark - override
- (void)didInitialize {
    [super didInitialize];
    
    self.sourceType = NSMAssetSourceTypeAll;
    self.albums = @[].mutableCopy;
}

#pragma mark - initialization
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTableView];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
        self.tableView.sourceManager.dataArray = [_assetsManager fecthAssetGroupsWithSourceType:self.sourceType];
#pragma clang diagnostic pop
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    });
}

- (void)initTableView {
    self.tableView.sourceManager.classForCellAtIndexPath = ^Class(NSIndexPath *indexPath) {
        return NSMAssetCell.class;
    };
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSMAssetsGroup *assetsGroup = tableView.sourceManager.dataArray[indexPath.row];
    NSMAssetPickerController *vc = NSMAssetPickerController.new;
    vc.assetsGroup = assetsGroup;
    @nsm_weak(self)
    vc.didSelectItem = ^(NSMAsset * _Nonnull asset) {
        @nsm_strong(self)
        UIViewController *tempVC = NSClassFromString(@"NSMAssetBrowserController").new;
        [self.navigationController pushViewController:tempVC animated:YES];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - setter/getter

#pragma mark - network

#pragma mark - method/public

#pragma mark - method/private

#pragma mark - action

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
